{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Number of Steps to Make Two Strings Anagram II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minSteps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #使两字符串互为字母异位词的最少步骤数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个字符串 <code>s</code> 和 <code>t</code> 。在一步操作中，你可以给 <code>s</code> 或者 <code>t</code> 追加 <strong>任一字符</strong> 。</p>\n",
    "\n",
    "<p>返回使 <code>s</code> 和 <code>t</code> 互为 <strong>字母异位词</strong> 所需的最少步骤数<em>。</em></p>\n",
    "\n",
    "<p><strong>字母异位词 </strong>指字母相同但是顺序不同（或者相同）的字符串。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"<em><strong>lee</strong>t</em>co<em><strong>de</strong></em>\", t = \"co<em><strong>a</strong></em>t<em><strong>s</strong></em>\"\n",
    "<strong>输出：</strong>7\n",
    "<strong>解释：</strong>\n",
    "- 执行 2 步操作，将 \"as\" 追加到 s = \"leetcode\" 中，得到 s = \"leetcode<em><strong>as</strong></em>\" 。\n",
    "- 执行 5 步操作，将 \"leede\" 追加到 t = \"coats\" 中，得到 t = \"coats<em><strong>leede</strong></em>\" 。\n",
    "\"leetcodeas\" 和 \"coatsleede\" 互为字母异位词。\n",
    "总共用去 2 + 5 = 7 步。\n",
    "可以证明，无法用少于 7 步操作使这两个字符串互为字母异位词。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"night\", t = \"thing\"\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>给出的字符串已经互为字母异位词。因此，不需要任何进一步操作。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length, t.length &lt;= 2 * 10<sup>5</sup></code></li>\n",
    "\t<li><code>s</code> 和 <code>t</code> 由小写英文字符组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-number-of-steps-to-make-two-strings-anagram-ii](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-number-of-steps-to-make-two-strings-anagram-ii](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"leetcode\"\\n\"coats\"', '\"night\"\\n\"thing\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        table=[0]*26\n",
    "        for ch in s:\n",
    "            table[ord(ch)-ord('a')]+=1\n",
    "        for ch in t:\n",
    "            table[ord(ch)-ord('a')]-=1\n",
    "        ans=0\n",
    "        for i in range(26):\n",
    "            ans+=abs(table[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        dic = {}\n",
    "        for i in s:\n",
    "            if i in dic:\n",
    "                dic[i] += 1\n",
    "            else:\n",
    "                dic[i] = 1\n",
    "        for i in t:\n",
    "            if i in dic:\n",
    "                dic[i] -= 1\n",
    "            else:\n",
    "                dic[i] = -1\n",
    "        ans = 0\n",
    "        for key in dic:\n",
    "            ans += abs(dic[key])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        from collections import Counter\n",
    "        d1,d2 = Counter(s),Counter(t)\n",
    "        res = 0\n",
    "        for k1 in d1.keys():\n",
    "            if k1 not in d2.keys():\n",
    "                res += d1[k1]\n",
    "            else:\n",
    "                res += abs(d1[k1]-d2[k1])\n",
    "        for k2 in d2.keys():\n",
    "            if k2 not in d1.keys():\n",
    "                res += d2[k2]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        dic1 = Counter(s)\n",
    "        dic2 = Counter(t)\n",
    "        a =sum((dic1-dic2).values()) \n",
    "        b =sum((dic2-dic1).values()) \n",
    "        return a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        temps=[0]*26\n",
    "        tempt=[0]*26\n",
    "        ans=0\n",
    "        for c in s:\n",
    "            temps[ord(c)-ord('a')]+=1\n",
    "        for c in t:\n",
    "            tempt[ord(c)-ord('a')]+=1\n",
    "        for i in range(26):\n",
    "            ans+=abs(temps[i]-tempt[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        ans = [0] * 26\n",
    "        for i in s:\n",
    "            ans[ord(i) - ord('a')] += 1\n",
    "        for i in t:\n",
    "            ans[ord(i) - ord('a')] -= 1 \n",
    "        res = 0\n",
    "        for i in range(len(ans)):\n",
    "            res += abs(ans[i])\n",
    "        return res    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        dic1 = Counter(s)\n",
    "        dic2 = Counter(t)\n",
    "        a =sum((dic1-dic2).values()) \n",
    "        b =sum((dic2-dic1).values()) \n",
    "        return a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        c1 = Counter(s)\n",
    "        c2 = Counter(t)\n",
    "        ret = 0\n",
    "        for k, v in c1.items():\n",
    "            ret += abs(v - c2[k])\n",
    "        for k, v in c2.items():\n",
    "            if k not in c1:\n",
    "                ret += c2[k]\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        res = 0\n",
    "        ls,lt = [0] * 26,[0] * 26\n",
    "        for i in s:\n",
    "            ls[ord(i)-ord('a')] += 1\n",
    "        for i in t:\n",
    "            lt[ord(i)-ord('a')] += 1\n",
    "        for a,b in zip(ls,lt):\n",
    "            res += abs(a-b)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        mp1 = Counter(s)\n",
    "        mp2 = Counter(t)\n",
    "        mp3 = (mp1 | mp2) - (mp1 & mp2)   # 并集减去交集\n",
    "        return sum(mp3.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        cnt = [0] * 26\n",
    "        ans = 0\n",
    "        for x in s:\n",
    "            cnt[ord(x) - ord(\"a\")] += 1\n",
    "        for x in t:\n",
    "            cnt[ord(x) - ord(\"a\")] -= 1\n",
    "        for x in cnt:\n",
    "            ans += abs(x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        mp1 = Counter(s)\n",
    "        mp2 = Counter(t)\n",
    "\n",
    "        mp3 = (mp1 | mp2) - (mp1 & mp2)   # 并集减去交集\n",
    "\n",
    "        return sum(mp3.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        cnts = Counter(s)\n",
    "        cntt = Counter(t)\n",
    "\n",
    "        ans = 0\n",
    "        for k, v in cnts.items():\n",
    "            ans += abs(v - cntt[k])\n",
    "            cntt[k] = 0\n",
    "\n",
    "        return ans + sum(cntt.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        c = Counter(s)\n",
    "        c.subtract(Counter(t))\n",
    "        return sum(map(abs, c.values()))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        freq1 = collections.Counter(s)\n",
    "        freq2 = collections.Counter(t)\n",
    "        return sum(((freq1 | freq2) - (freq1 & freq2)).values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        rst = [0] * 26\n",
    "        base_a = ord('a')\n",
    "        total = 0\n",
    "\n",
    "        for c in s:\n",
    "            rst[ord(c) - base_a] += 1\n",
    "            total += 1\n",
    "\n",
    "        for c in t:\n",
    "            i = ord(c) - base_a\n",
    "\n",
    "            if rst[i] <= 0:\n",
    "                total += 1\n",
    "            else:\n",
    "                total -= 1\n",
    "\n",
    "            rst[i] -= 1\n",
    "\n",
    "        return total\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        cnt_s, cnt_t = Counter(s), Counter(t)\n",
    "        ans = (cnt_s | cnt_t) - (cnt_s & cnt_t)\n",
    "        return sum(ans.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        a= Counter(s)\n",
    "        b=Counter(t)\n",
    "        value=(a|b)-(a&b)\n",
    "        print(value)\n",
    "        return sum(value.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        dic = defaultdict(int)\n",
    "        for c in s:\n",
    "            dic[c] += 1\n",
    "        for c in t:\n",
    "            dic[c] -= 1\n",
    "        ans = 0\n",
    "        for v in list(dic.values()):\n",
    "            ans += abs(v)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        dic1 = Counter(s)\n",
    "        dic2 = Counter(t)\n",
    "        a =sum((dic1-dic2).values()) \n",
    "        b =sum((dic2-dic1).values()) \n",
    "        return a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        a = Counter(s)\n",
    "        b = Counter(t)\n",
    "        return sum((a - b).values()) + sum((b - a).values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        dicT,dicS=Counter(t),Counter(s)\n",
    "\n",
    "        ret=(dicS | dicT)-(dicS & dicT)\n",
    "        \n",
    "        return sum(ret.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        d1 = collections.defaultdict(int)\n",
    "        d2 = collections.defaultdict(int)\n",
    "        for c in s:\n",
    "            d1[c] += 1\n",
    "        for c in t:\n",
    "            d2[c] += 1\n",
    "\n",
    "        ret = 0\n",
    "        for c in d1:\n",
    "            if d1[c] > d2[c]:\n",
    "                ret += d1[c] - d2[c]\n",
    "        for c in d2:\n",
    "            if d2[c] > d1[c]:\n",
    "                ret += d2[c] - d1[c]\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        '''\n",
    "\n",
    "        '''\n",
    "        map1 = Counter(s)\n",
    "        map2 = Counter(t)\n",
    "\n",
    "        map3 = (map1 | map2) - (map1 & map2)\n",
    "\n",
    "        return sum(map3.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        nums = [0 for i in range(26)]\n",
    "        for l in s:\n",
    "            nums[ord(l) - 97] += 1\n",
    "        for l in t:\n",
    "            nums[ord(l) - 97] -= 1\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            ans += abs(num)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        dp1 = [ 0 ] * 26\n",
    "        dp2 = [ 0 ] * 26\n",
    "        ans = 0\n",
    "        for i in range(len(s)):\n",
    "            dp1[ord(s[i])- ord('a')] += 1\n",
    "        \n",
    "        for j in range(len(t)):\n",
    "            dp2[ord(t[j])- ord('a')] += 1\n",
    "        \n",
    "        for k in range(26):\n",
    "            ans += abs(dp1[k]-dp2[k])\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        from collections import Counter\n",
    "        s_counter, t_counter = Counter(s), Counter(t)\n",
    "        return sum((s_counter - t_counter).values()) + sum((t_counter - s_counter).values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        s_map, t_map = Counter(s), Counter(t)\n",
    "        return sum(abs(s_map[key] - t_map[key])   for key in s_map.keys() | t_map.keys())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        s_dict, t_dict = {}, {}\n",
    "        for c in s:\n",
    "            s_dict[c] = 1 + s_dict.get(c, 0)\n",
    "        for c in t:\n",
    "            t_dict[c] = 1 + t_dict.get(c, 0)\n",
    "\n",
    "        diff = 0\n",
    "        for key in s_dict:\n",
    "            diff += max(s_dict[key]-t_dict.get(key, 0), 0)\n",
    "        for key in t_dict:\n",
    "            diff += max(t_dict[key]-s_dict.get(key, 0), 0)\n",
    "\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        ans = 0\n",
    "        cnt1, cnt2 = collections.Counter(s), collections.Counter(t)\n",
    "        for k, v in cnt1.items():\n",
    "            if k not in cnt2.keys():\n",
    "                ans += v\n",
    "            elif v > cnt2[k]:\n",
    "                diff = v - cnt2[k]\n",
    "                ans += diff\n",
    "                cnt2[k] += diff\n",
    "        \n",
    "        for k, v in cnt2.items():\n",
    "            if k not in cnt1.keys():\n",
    "                ans += v\n",
    "            elif v > cnt1[k]:\n",
    "                diff = v - cnt1[k]\n",
    "                ans += diff\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        a={}\n",
    "        b={}\n",
    "\n",
    "        for x in s:\n",
    "            a[x]=a.get(x,0)+1\n",
    "\n",
    "        for x in t:\n",
    "            b[x]=b.get(x,0)+1\n",
    "        \n",
    "        same=0\n",
    "\n",
    "        for i in range(ord('a'),ord('z')+1):\n",
    "            same+=min(a.get(chr(i),0), b.get(chr(i),0))\n",
    "\n",
    "        return len(s)+len(t)-2*same\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        s_count = Counter(s)\n",
    "        t_count = Counter(t)\n",
    "        print(s_count, t_count)\n",
    "\n",
    "        result = 0\n",
    "        for key, value in s_count.items():\n",
    "            if key not in t_count:\n",
    "                result += value\n",
    "            else:\n",
    "                if value != t_count[key]:\n",
    "                    result += abs(value - t_count[key])\n",
    "        \n",
    "\n",
    "        for key, value in t_count.items():\n",
    "            if key not in s_count:\n",
    "                result += value\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        rst = [0] * 26\n",
    "        bit_map = 0\n",
    "        base_a = ord('a')\n",
    "\n",
    "        for c in s:\n",
    "            rst[ord(c) - base_a] += 1\n",
    "\n",
    "        for c in t:\n",
    "            i = ord(c) - base_a\n",
    "            bit_mask = 1 << i\n",
    "\n",
    "            if rst[i] == 0:\n",
    "                bit_map |= bit_mask\n",
    "\n",
    "            if bit_map & bit_mask:\n",
    "                rst[i] += 1\n",
    "            else:\n",
    "                rst[i] -= 1\n",
    "\n",
    "        return sum(rst)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        dic1 = Counter(s)\n",
    "        dic2 = Counter(t)\n",
    "        a = dic1-dic2\n",
    "        b = dic2-dic1\n",
    "        dic3 = a+b\n",
    "        return sum(dic3.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        freq1 = collections.Counter(s)\n",
    "        freq2 = collections.Counter(t)\n",
    "        ans = 0\n",
    "        for key, value in freq1.items():\n",
    "            if key not in freq2:\n",
    "                ans += value\n",
    "                freq2[key] = value\n",
    "            else:\n",
    "                if value > freq2[key]:\n",
    "                    ans += (value - freq2[key])\n",
    "                    freq2[key] = value\n",
    "\n",
    "\n",
    "        for key, value in freq2.items():\n",
    "            if key not in freq1:\n",
    "                ans += value\n",
    "                freq1[key] = value\n",
    "            else:\n",
    "                if value > freq1[key]:\n",
    "                    ans += value - freq1[key]\n",
    "                    freq1[key] = value\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        c1, c2 = Counter(s), Counter(t)\n",
    "        ans = 0\n",
    "        for k in c1 | c2:\n",
    "            ans += abs(c1[k] - c2[k])\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        # 并集 - 交集\n",
    "        cnts = Counter(s)\n",
    "        cntt = Counter(t)\n",
    "        res = 0\n",
    "        for k,v in cnts.items():\n",
    "            if k not in cntt:\n",
    "                res += v\n",
    "            else:\n",
    "                if v<cntt[k]:\n",
    "                    res += (cntt[k] - v)\n",
    "        \n",
    "        for k,v in cntt.items():\n",
    "            if k not in cnts:\n",
    "                res += v\n",
    "            else:\n",
    "                if v<cnts[k]:\n",
    "                    res += (cnts[k] - v)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        sCount = Counter(s)\n",
    "        tCount = Counter(t)\n",
    "        ans = 0\n",
    "        \n",
    "        for item in sCount:\n",
    "            if item not in tCount:\n",
    "                ans += sCount[item]\n",
    "            else:\n",
    "                ans += abs(sCount[item] - tCount[item])\n",
    "        \n",
    "        for item in tCount:\n",
    "            if item not in sCount:\n",
    "                ans += tCount[item]\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # # 哈希+计数+贪心\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        cnt1 = Counter(s)\n",
    "        cnt2 = Counter(t)\n",
    "        ans = 0\n",
    "        for i in range(26):\n",
    "            w = chr(i + ord(\"a\"))\n",
    "            ans += abs(cnt1[w] - cnt2[w])\n",
    "            \n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        counterS=dict()\n",
    "        counterT=dict()\n",
    "        for ch in s+t:\n",
    "            counterS[ch]=0\n",
    "            counterT[ch]=0\n",
    "        for ch in s:\n",
    "            counterS[ch]+=1\n",
    "        for ch in t:\n",
    "            counterT[ch]+=1\n",
    "        ans=0\n",
    "        for key in counterS:\n",
    "            ans+=max(counterS[key],counterT[key])-min(counterS[key],counterT[key])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minSteps(self, s: str, t: str) -> int:\n",
    "        cs = Counter(s)\n",
    "        ct = Counter(t)\n",
    "        ans = 0\n",
    "        for i in range(26):\n",
    "            c = chr(ord('a') + i)\n",
    "            ans += abs(cs[c] - ct[c])\n",
    "        return ans\n",
    "            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
